/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","ajaxServ","sysmanMain", "bizProto","btnServ","flowServ","formServ"],
		function($,datagridServ,ajaxServ, sysmanMain, bizProto,btnServ,flowServ,formServ) {

			'use strict';

			var _bizProto,_moduleConfig,_buttonConfig,_defaultConfig,_rootpath,
				//锚记datagrid目标元素
				record,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,$edit_window_height=700,$edit_window_width=700,window_btn,
				mainUrl = '/medicalInpatient/query/getListDomain.action',
				itemUrl = '/medicalInpatientItem/query/getListDomain.action',
				feeUrl = '/medicalInpatientFee/query/getListDomain.action',
				//流程执行信息grid
				_historicGrid,
				//业务表单对象
				$form,_defaultConfig,
				_moduCode = 'inpatientItemFee',
				item_dic_code=100,
				dicpath='/dictionary/getTree.action'
				
				
				
			var public_method = {				
				
				init:function(_options){
					
					_defaultConfig ={
						doc:null,
						rootId:null,				
						panelSelector:'div.well',
						subSystemCode:null,
						moduleCode:null,
						editWindow:null,
						btnSelector:'div.flow-edit-btn',
						initQueryParams:null,
						initElement:null,
						historicUrl:null,
						historicField:null
					}
					
					_moduleConfig = sysmanMain.getModuConf(_moduCode);
					if (window.console) {
						console.info(_moduCode + ' modules gets ready and been loaded.');
					}
					
					
					_defaultConfig = $.extend({},_defaultConfig,_moduleConfig,_options)			
					_bizProto = new bizProto(_defaultConfig);			
					_bizProto.gridHeight -= 15
					_rootpath = _bizProto.rootPath
					_moduleConfig.bizProto=_bizProto
					_moduleConfig.defaultConfig = _defaultConfig
					
					
					
					if(_defaultConfig.editWindow){
						$edit_window = $('#'+_defaultConfig.editWindow)
						_moduleConfig.edit_window = _defaultConfig.editWindow
					}
					
					_buttonConfig={										    
						 "查看项目":private_method.queryItem,
						 "查看费用":private_method.queryFee,
						 "取消":private_method.cancel						 
					}
					
					_moduleConfig.buttonConfig = _buttonConfig
					//保存模块初始化信息
					sysmanMain.setModuConf(_moduCode,_moduleConfig)
					if(_defaultConfig.initQueryParams){
						
					}
					if(_defaultConfig.initQueryParams)
						ajaxServ.runAjax({
							url:_moduleConfig.bizProto.rootPath + mainUrl,
							contentType:'form',
							data:_defaultConfig.initQueryParams,
							callback:private_method.init})
										
				}
				
			}

			var private_method = {
				
				//初始化元素
				init : function(data) {
					record = data
					// 编辑窗口初始化									
					$form = $('#'+_defaultConfig.rootId + ' form.main-form')
					$form.form('clear')
					
					var _curRow
					if(record && record.rows && record.rows[0]){
						_curRow = record.rows[0]					
						
						var _url = '/'+sysmanMain.getRootPath()+'/medicalInpatientDiagnosis/query/getDiagnosis.action';
						
						//患者性别
						if(_curRow.patient_sex==1){
							_curRow.sex = '男'
						}else{
							_curRow.sex = '女'
						}
						
						if(_curRow.patient_birth && _curRow.inhzospitalat){
							var bir = _curRow.patient_birth.split('-')
							var fee = _curRow.inhzospitalat.split('-')
							_curRow.age=_curRow.patient_birth +'  ,年龄: '+(fee[0]-bir[0])+' 周岁';
						}
						//判断是否存在诊断信息
						if(!_curRow.diagnosis){
							$.ajax({
								url   : _url,
								data  : {id:_curRow.patient_id},
								async : false,
								success:function(data){
									if(data){
										_curRow.diagnosis_in = data.diagnosis_in;
										_curRow.diagnosis_out = data.diagnosis_out;
									}
								},
								error:function(){
									
								}
							})
						}
					}
					
					$form.form('load',record.rows[0])
					
					//绑定按钮事件					
					private_method.bindBtnEvent();					
					
					//加载流程执行信息
									
				},
				
				//绑定按钮事件
				bindBtnEvent:function(){
					
					window_btn = $($edit_window).find('a.function-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					
				},				
				
				// 功能按钮监听函数
				function_btn_listenner : function(e) {

					btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

				},
				
				queryItem:function(){
					_moduleConfig = sysmanMain.getModuConf(_moduCode);
					$('#inpatient-item-fee div.fee-panel').panel('close')
					var _itemPanel = $('#inpatient-item-fee div.item-panel')
					_itemPanel.panel('open')
					var $grid  =$('#inpatient-item-fee table.item-list')
					var _options = $($grid).datagrid('options')
					var _condition = _moduleConfig.initQueryParams.condition
					if(_options && _options.url){
						$grid.datagrid('load',{condition:_condition})
					}else{
						var _initParam = {
							url:_bizProto.rootPath + itemUrl,
							queryParams:{condition:_condition}
						}
						
						$grid.datagrid(_initParam)
					}
					private_method.initTreatItem()
					
					var _btn = $('#inpatient-item-fee div.condition-form-item a.function-btn')
					$(_btn).unbind('click');
					$(_btn).bind('click', private_method.clickItemBtn)	
				},
				
				queryFee:function(){
					_moduleConfig = sysmanMain.getModuConf(_moduCode);
					$('#inpatient-item-fee div.item-panel').panel('close')
					
					var _feePanel = $('#inpatient-item-fee div.fee-panel')
					_feePanel.panel('open')
					var $grid  =$('#inpatient-item-fee table.fee-list')
					var _options = $($grid).datagrid('options')
					var _condition = _moduleConfig.initQueryParams.condition
					if(_options && _options.url){
						$grid.datagrid('load',{condition:_condition})
					}else{
						var _initParam = {
							url:_bizProto.rootPath + feeUrl,
							queryParams:{condition:_condition}
						}
						
						$grid.datagrid(_initParam)
					}
					
					private_method.initFeeItem()
					
					var _btn = $('#inpatient-item-fee div.condition-form-fee a.function-btn')
					$(_btn).unbind('click');
					$(_btn).bind('click', private_method.clickFeeBtn)
				},
				//
				cancel : function() {
					$($edit_window).window('close');
				},
				
				/**
				 * @param _options.rootId
				 * @param _options.form
				 * @param _options.nameEl_id
				 * @param _options.valueEl_id
				 */
				initTreatItem:function(_options){
					var _mainSelect = $('#inpatient-item-fee div.condition-form-item #drug_code_in')
					if(_mainSelect && _mainSelect.length>0){
						$(_mainSelect).combobox({
							textField:'name',
							valueField:'dic_value',
							url:_rootpath + dicpath +'?id=' + item_dic_code,
							onSelect:function(record){}
						})
					}
				},
				
				/**
				 * @param _options.rootId
				 * @param _options.form
				 * @param _options.nameEl_id
				 * @param _options.valueEl_id
				 */
				initFeeItem:function(_options){
					var _mainSelect = $('#inpatient-item-fee div.condition-form-fee #drug_code_in')
					if(_mainSelect && _mainSelect.length>0){
						$(_mainSelect).combobox({
							textField:'name',
							valueField:'dic_value',
							url:_rootpath + dicpath+'?id=' + item_dic_code,
							onSelect:function(record){}
							
						})
					}
				},
				
				clickFeeBtn:function(){
					var _feeForm = $('#inpatient-item-fee div.condition-form-fee form')
					var _condition = formServ.getDataObj({form:_feeForm})
					_condition.patient_id=private_method.getPatientId();
					var _grid = $('#inpatient-item-fee table.fee-list')
					$(_grid).datagrid('load',{condition:_condition})
				},
				clickItemBtn:function(){
					var _itemForm = $('#inpatient-item-fee div.condition-form-item form')
					var _condition = formServ.getDataObj({form:_itemForm})
					_condition.patient_id=private_method.getPatientId();
					var _grid = $('#inpatient-item-fee table.item-list')
					$(_grid).datagrid('load',{condition:_condition})
				},
				getPatientId:function(){
					var patientid = $('#inpatient-item-fee form.main-form #patient_id')
					return $(patientid).val()
				}
				
				

			};
			
			return public_method;

		});